package com.xxd.rpt2.controller;

import com.xxd.rpt2.Entity.User;
import com.xxd.rpt2.service.UserService;
import com.xxd.rpt2.vo.ResultModel;
import com.xxd.utils.PasswordHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("public")
public class PublicController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "login", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel login(@RequestParam(name = "username") String username,
                             @RequestParam(name = "password") String password) {
        Subject subject = SecurityUtils.getSubject();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);
            User user = (User) subject.getPrincipal();
            String newToken = userService.signToken(user.getCode(), user.getSalt());
            Map map = new HashMap();
            map.put("xd-token",newToken);
            return new ResultModel(map);
        } catch (Exception e) {
            return new ResultModel(HttpStatus.UNAUTHORIZED,e.getMessage());
        }
    }

    @RequestMapping(value = "logout", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel logout() {
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            return new ResultModel();
        } catch (Exception e) {
            return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR,e.getMessage());
        }
    }

    @RequestMapping(value = "register", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel register(String username, String password) {
        try {
            User user = userService.findByName(username);
            if (user !=null) {
                return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, "用户名已存在");
            } else {
                user = new User();
            }
            PasswordHelper passwordHelper = new PasswordHelper();
            user.setCode(username);
            user.setPassword(password);
            passwordHelper.encryptPassword(user);
            userService.saveUser(user);
            return new ResultModel(true);
        } catch (Exception e) {
            return new ResultModel(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }


}
